package com.hanyi.mvc.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Object user = request.getSession().getAttribute("user");
        System.out.println("user = " + user);
        if (user != null){
            // 登录放行
            return true;
        }
        request.setAttribute("msg","请先登录");
        request.getRequestDispatcher("/login.jsp").forward(request,response);
        return false;
    }
}
